package com.yifeng.repo.base.utils.common;

import java.util.UUID;

/**
 * Created by daibing on 2018/8/9.
 */
public class UUIDExt {
    public static String[] charSeeds = new String[] { "a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z" };

    public static String[] numberSeeds = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

    public static String generate(String[] seeds) {
        StringBuilder sb = new StringBuilder();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            sb.append(seeds[x % seeds.length]);
        }
        return sb.toString();
    }

    public static String generate(String[] seeds, int times) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < times; i++) {
            sb.append(generate(seeds));
        }
        return sb.toString();
    }

    public static String generateAccessKeyId() {
        return generate(charSeeds, 2);
    }

    public static String generateAccessKeySecret() {
        return generate(charSeeds, 4);
    }

    public static void main(String[] args) {
        System.out.println("generate AccessKeyId    : " + generateAccessKeyId());
        System.out.println("generate AccessKeySecret: " + generateAccessKeySecret());
    }
}
